package com.example.controller;

import com.example.Constants.SystemConstants;
import com.example.domain.ResponseResult;
import com.example.domain.dto.IdNameDto;
import com.example.domain.entity.Role;
import com.example.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @PostMapping("/list")
    @PreAuthorize("@ps.hasPermission('roleList')")
    public ResponseResult getRoleList(){
        return roleService.getRoleList();
    }

    @PostMapping("/delete")
    @PreAuthorize("@ps.hasPermission('roleDelete')")
    public ResponseResult deleteRole(@RequestBody Map<String,Integer> map){
        Integer id = map.get("id");
        return roleService.deleteRole(id);
    }

    @PostMapping("/add")
    @PreAuthorize("@ps.hasPermission('roleAdd')")
    public ResponseResult addRole(@RequestBody Map<String,String> map){
        String roleName = map.get("name");
        return roleService.addRole(roleName);
    }
    @PostMapping("/modInit")
    @PreAuthorize("@ps.hasPermission('roleEdit')")
    public ResponseResult modInit(@RequestBody Map<String,Integer> map){
        Integer id = map.get("id");
        return roleService.modInit(id);
    }

    //修改职能名称
    @PostMapping("/mod")
    @PreAuthorize("@ps.hasPermission('roleEdit')")
    public ResponseResult modRole(@Valid @RequestBody IdNameDto idNameDto){
        return roleService.modRole(idNameDto);
    }
}
